﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using LINQPad.OpenAccess.Helpers;

namespace LINQPad.OpenAccess.Metadata.Reflection.Locators
{
    /// <summary>
    /// Combined <see cref="IMetadataSourceLocator"/>
    /// </summary>
    public class CombinedMetadataSourceLocator : IMetadataSourceLocator
    {
        #region Private

        #region - Vars

        [DebuggerBrowsable( DebuggerBrowsableState.Never )]
        private readonly IMetadataSourceLocator[] _locators;

        #endregion

        #endregion

        #region Public

        #region - Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CombinedMetadataSourceLocator" /> class.
        /// </summary>
        /// <param name="locators">The locators.</param>
        public CombinedMetadataSourceLocator( IEnumerable<IMetadataSourceLocator> locators )
        {
            Guard.ArgumentNotNull( locators, "locators" );
            this._locators = locators.ToArray( );
        }

        #endregion

        #region - Properties

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <value>
        /// The count.
        /// </value>
        public int Count
        {
            get
            {
                return this._locators.Length;
            }
        }

        #endregion

        #endregion

        #region IMetadataSourceLocator Members

        /// <summary>
        /// Gets the metadata source infos from the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns>
        /// The metadata source infos.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IEnumerable<IMetadataSourceInfoWithSource> GetFromAssembly( Assembly assembly )
        {
            return this._locators
                       .SelectMany( l => l.GetFromAssembly( assembly ) );
        }

        #endregion
    }
}
